package wicresoft.controller.expertInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.expertInfo.ExpertBaseServices;
import util.DateUtil;
import util.key.IDFactory;
import wicresoft.controller.common.CommonResult;
import wicresoft.orm.entity.ExpertBaseInfo;
import wicresoft.orm.entity.SharePerson;
import wicresoft.orm.entity.UnitUser;
import wicresoft.services.expertInfo.ExpertBaseInfoServices;
import wicresoft.services.expertInfo.SharePersonServices;
import wicresoft.services.expertInfo.SpecialSqlServices;
import wicresoft.services.expertInfo.UnitUserServices;
import wt.entity.expertInfo.ExpertBase;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("httpPage")
public class CreateLoginAccountController {
    Logger log = LoggerFactory.getLogger(CreateLoginAccountController.class);

    @Autowired
    SharePersonServices sharePersonServices;
    @Autowired
    UnitUserServices unitUserServices;
    @Autowired
    ExpertBaseInfoServices expertBaseInfoServices;
    @Autowired
    SpecialSqlServices specialSqlServices;
    @Autowired
    ExpertBaseServices expertBaseServices;

    @ResponseBody
    @RequestMapping(value = "getLoginInfo")
    public CommonResult  getLoginInfo(String idCard) throws Exception {
        CommonResult commonResult = new CommonResult();
        Map<String,String> data=new HashMap<String, String>();

        UnitUser unitUser = unitUserServices.selectByIdCard(idCard);
        String password = null;
        String account = null;
        if (unitUser != null) {
            account = unitUser.getUserId();
            Map map = specialSqlServices.specialSelect("select  decrypt('" + unitUser.getPassword() + "')   PASSWORD from dual");//解密
            if (map != null && map.containsKey("PASSWORD")) {
                password = map.get("PASSWORD").toString();
            }
        }

        if (password == null || account == null) {
            commonResult.setCode(CommonResult.ZJ005);
            commonResult.setMessage( unitUser == null ? "通过身份证号无法从老专家系统获取专家信息" : "获取专家信息");
        } else {
            data.put("password",password);
            data.put("account",account);
            commonResult.setCode(CommonResult.ZJ001);
            commonResult.setMessage(CommonResult.ZJ001_M);
            commonResult.setData(data);
        }
        return  commonResult;



    }


    @RequestMapping(value = "createExpertAccount")
    public synchronized CommonResult createExpertAccount( String idCard) {
        CommonResult commonResult = new CommonResult();
        try {
            ExpertBaseInfo expertBaseInfo = expertBaseInfoServices.selectByIdCard(idCard);
            if (expertBaseInfo != null) {
                commonResult.setCode(CommonResult.ZJ001);
                commonResult.setMessage(CommonResult.ZJ001_M);
                log.error("老专家系统已经存在该专家{}", expertBaseInfo.getIdcard());
            } else {
                ExpertBase expertBase= expertBaseServices.selectByIdCard(idCard);
                expertBaseInfo = createExpertBaseInfo(expertBase.getName(), expertBase.getIdCard(), expertBase.getPhone(), "123456");
                createSharePerson(expertBaseInfo);
                createUnitUser(expertBaseInfo);
                commonResult.setCode(CommonResult.ZJ001);
                commonResult.setMessage(CommonResult.ZJ001_M);
            }
        } catch (Exception e) {
            commonResult.setCode(CommonResult.ZJ000);
            commonResult.setMessage(e.getMessage());
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return commonResult;

    }

    private ExpertBaseInfo createExpertBaseInfo(String name, String idCard, String phoneNum, String password) throws Exception {
        ExpertBaseInfo expertBaseInfo = new ExpertBaseInfo();
        Map map = specialSqlServices.specialSelect("select  pid+1 ID  from (select  * from expertbaseinfo   order by pid desc) where  rownum =1");
        Long id = null;
        if (map != null && map.containsKey("ID")) {
            try {
                id = Long.valueOf(map.get("ID").toString());
            } catch (Exception e) {
                log.error("【Manual】=>{}", e.getMessage());
                id = Long.valueOf(IDFactory.getSnowflakeId().substring(10));
            }
        }

        expertBaseInfo.setPid(id);
        expertBaseInfo.setAvailability(-1L);
        expertBaseInfo.setCel(phoneNum);
        expertBaseInfo.setIdcard(idCard);
        expertBaseInfo.setIsidcard(0L);
        expertBaseInfo.setPsd(password);
        expertBaseInfo.setUsername("ZJW" + expertBaseInfo.getPid());
        expertBaseInfo.setExpertId("ZJSYS_" + expertBaseInfo.getPid());
        expertBaseInfo.setExpertname(name);
        expertBaseInfo.setQysh(-1L);
        expertBaseInfo.setSfrk(-1L);
        expertBaseInfo.setTysh(-1L);
        expertBaseInfoServices.insertSelective(expertBaseInfo);
        return expertBaseInfo;

    }

    private SharePerson createSharePerson(ExpertBaseInfo expertBaseInfo) throws Exception {
        SharePerson sharePerson = new SharePerson();
        sharePerson.setId(expertBaseInfo.getExpertId());
        sharePerson.setIdCard(expertBaseInfo.getIdcard());
        sharePerson.setName(expertBaseInfo.getExpertname());
        sharePerson.setStatus("1");
        sharePerson.setMobile(expertBaseInfo.getCel());
        sharePersonServices.insertSelective(sharePerson);
        return sharePerson;
    }


    private UnitUser createUnitUser(ExpertBaseInfo expertBaseInfo) throws Exception {
        UnitUser unitUser = new UnitUser();
        unitUser.setId("ZJUSER_" + expertBaseInfo.getPid());
        unitUser.setUnitId(expertBaseInfo.getExpertId());
        unitUser.setUserId(expertBaseInfo.getUsername());

        String password = "D9188C3AE5FFE4463255787B91FE3932";//123456
//        Map map = specialSqlServices.specialSelect("select  encrypt('" + expertBaseInfo.getPsd() + "')   PASSWORD from dual");
//        if (map!=null&&map.containsKey("PASSWORD")){
//            password=map.get("PASSWORD").toString();
//        }

        unitUser.setPassword(password);
        unitUser.setPersonId(unitUser.getUnitId());
        unitUser.setUserType("ZJ");
        unitUser.setUserTable("SHARE_TALENT");
        unitUser.setNum("1");
        unitUser.setMobile(expertBaseInfo.getCel());
        unitUser.setUpdateTime(DateUtil.date2String(new Date(), DateUtil.DATE_TIME_PATTEN));
        unitUserServices.insertSelective(unitUser);
        return unitUser;

    }


}
